﻿<%@ Control Language="C#" AutoEventWireup="true" Inherits="Pages_Reservations_Ascx_PaymentPopup"
    CodeBehind="PaymentPopup.ascx.cs" %>
<%@ Register Src="~/Tools/NumericBox.ascx" TagName="NumericBox" TagPrefix="num" %>
<div id="payment-dialog" title="Payment" style="display: none">
    <table cellspacing="5">
        <tr>
            <td colspan="2">
                <div class="tabs" style="height: 186px; width: 638px;">
                    <ul class="tabNavigation">
                        <li><a href="#cash"><span>Cash</span></a></li>
                        <li><a href="#german"><span>German Acc.</span></a></li>
                        <li><a href="#onepay"><span>One Pay</span></a></li>
                        <li><a href="#chargeacc"><span>Trả sau</span></a></li>
                        <li><a href="#paypal"><span>Pay Pal</span></a></li>
                        <li><a href="#creditcard"><span>Credit Card</span></a></li>
                    </ul>
                    <!-- CASH -->
                    <div id="cash">
                        <table cellspacing="5">
                            <thead>
                                <tr>
                                    <th colspan="4">
                                        * Received one or more currency in the cash method.
                                    </th>
                                </tr>
                            </thead>
                            <tbody>
                                <tr>
                                    <td>
                                        Conversion from
                                        <asp:DropDownList ID="ddlCurrencyType1" runat="server" Width="68px">
                                        </asp:DropDownList>
                                    </td>
                                    <td>
                                        <num:NumericBox ID="txtAmount1" runat="server" />
                                        <asp:HiddenField ID="txtExRate1" runat="server" Value="1"></asp:HiddenField>
                                    </td>
                                    <td>
                                        to
                                    </td>
                                    <td>
                                        <num:NumericBox ID="txtVNDAmount1" runat="server" CssClass="subpaid" Enabled="false" />
                                    </td>
                                </tr>
                                <tr>
                                    <td>
                                        Conversion from
                                        <asp:DropDownList ID="ddlCurrencyType2" runat="server" Width="68px">
                                        </asp:DropDownList>
                                    </td>
                                    <td>
                                        <num:NumericBox ID="txtAmount2" runat="server" />
                                        <asp:HiddenField ID="txtExRate2" runat="server" Value="1"></asp:HiddenField>
                                    </td>
                                    <td>
                                        to
                                    </td>
                                    <td>
                                        <num:NumericBox ID="txtVNDAmount2" runat="server" CssClass="subpaid" Enabled="false" />
                                    </td>
                                </tr>
                                <tr>
                                    <td>
                                        Received by
                                    </td>
                                    <td colspan="3">
                                        <asp:TextBox ID="txtCashReceived" runat="server"></asp:TextBox>
                                    </td>
                                </tr>
                            </tbody>
                        </table>
                    </div>
                    <!-- GERMAN -->
                    <div id="german">
                        <table cellspacing="5">
                            <tbody>
                                <tr>
                                    <td>
                                        Conversion from
                                        <asp:DropDownList ID="ddlGermanCurrencyType" runat="server" Width="68px">
                                        </asp:DropDownList>
                                    </td>
                                    <td>
                                        <num:NumericBox ID="txtGermanAmount" runat="server" />
                                        <asp:HiddenField ID="txtGermanExRate" runat="server" Value="1"></asp:HiddenField>
                                    </td>
                                    <td>
                                        to
                                    </td>
                                    <td>
                                        <num:NumericBox ID="txtGermanVNDAmount" runat="server" CssClass="subpaid" Enabled="false" />
                                    </td>
                                </tr>
                                <tr>
                                    <td>
                                        Received by
                                    </td>
                                    <td colspan="3">
                                        <asp:TextBox ID="txtGermanReceived" runat="server"></asp:TextBox>
                                    </td>
                                </tr>
                            </tbody>
                        </table>
                    </div>
                    <!-- PAYONE -->
                    <div id="onepay">
                        <table cellspacing="5">
                            <thead>
                                <tr>
                                    <th colspan="4">
                                        * Received one or more currency in the onepay method.
                                    </th>
                                </tr>
                            </thead>
                            <tbody>
                                <tr>
                                    <td>
                                        Conversion from
                                        <asp:DropDownList ID="ddlOnepayCurrencyType1" runat="server" Width="68px">
                                        </asp:DropDownList>
                                    </td>
                                    <td>
                                        <num:NumericBox ID="txtOnepayAmount1" runat="server" />
                                        <asp:HiddenField ID="txtOnepayExRate1" runat="server" Value="0"></asp:HiddenField>
                                    </td>
                                    <td>
                                        to
                                    </td>
                                    <td>
                                        <num:NumericBox ID="txtOnepayVNDAmount1" runat="server" CssClass="subpaid" Enabled="false" />
                                    </td>
                                </tr>
                                <tr>
                                    <td>
                                        Conversion from
                                        <asp:DropDownList ID="ddlOnepayCurrencyType2" runat="server" Width="68px">
                                        </asp:DropDownList>
                                    </td>
                                    <td>
                                        <num:NumericBox ID="txtOnepayAmount2" runat="server" />
                                        <asp:HiddenField ID="txtOnepayExRate2" runat="server" Value="0"></asp:HiddenField>
                                    </td>
                                    <td>
                                        to
                                    </td>
                                    <td>
                                        <num:NumericBox ID="txtOnepayVNDAmount2" runat="server" CssClass="subpaid" Enabled="false" />
                                    </td>
                                </tr>
                                <tr>
                                    <td>
                                        Received by
                                    </td>
                                    <td colspan="3">
                                        <asp:TextBox ID="txtOnepayReceived" runat="server"></asp:TextBox>
                                    </td>
                                </tr>
                            </tbody>
                        </table>
                    </div>
                    <!-- CHARGE ACC -->
                    <div id="chargeacc">
                        <table cellspacing="5">
                            <thead>
                                <tr>
                                    <th colspan="4">
                                        * Received one or more currency in the charge acc. method.
                                    </th>
                                </tr>
                            </thead>
                            <tbody>
                                <tr>
                                    <td>
                                        Conversion from
                                        <asp:DropDownList ID="ddlChargeCurrencyType1" runat="server" Width="68px">
                                        </asp:DropDownList>
                                    </td>
                                    <td>
                                        <num:NumericBox ID="txtChargeAmount1" runat="server" />
                                        <asp:HiddenField ID="txtChargeExRate1" runat="server" Value="0"></asp:HiddenField>
                                    </td>
                                    <td>
                                        to
                                    </td>
                                    <td>
                                        <num:NumericBox ID="txtChargeVNDAmount1" runat="server" CssClass="subpaid" Enabled="false" />
                                    </td>
                                </tr>
                                <tr>
                                    <td>
                                        Conversion from
                                        <asp:DropDownList ID="ddlChargeCurrencyType2" runat="server" Width="68px">
                                        </asp:DropDownList>
                                    </td>
                                    <td>
                                        <num:NumericBox ID="txtChargeAmount2" runat="server" />
                                        <asp:HiddenField ID="txtChargeExRate2" runat="server" Value="0"></asp:HiddenField>
                                    </td>
                                    <td>
                                        to
                                    </td>
                                    <td>
                                        <num:NumericBox ID="txtChargeVNDAmount2" runat="server" CssClass="subpaid" Enabled="false" />
                                    </td>
                                </tr>
                                <tr>
                                    <td>
                                        Received by
                                    </td>
                                    <td colspan="3">
                                        <asp:TextBox ID="txtChargeReceived" runat="server"></asp:TextBox>
                                    </td>
                                </tr>
                            </tbody>
                        </table>
                    </div>
                    <!-- PAY PAL -->
                    <div id="paypal">
                        <table cellspacing="5">
                            <tbody>
                                <tr>
                                    <td>
                                        Conversion from
                                    </td>
                                    <td>
                                        <asp:DropDownList ID="ddlPaypalCurrencyType" runat="server" Width="68px">
                                        </asp:DropDownList>
                                        <num:NumericBox ID="txtPaypalAmount" runat="server" />
                                        <asp:HiddenField ID="txtPaypalExRate" runat="server" Value="0"></asp:HiddenField>
                                    </td>
                                    <td>
                                        to
                                    </td>
                                    <td>
                                        <num:NumericBox ID="txtPaypalVNDAmount" runat="server" CssClass="subpaid" Enabled="false" />
                                    </td>
                                </tr>
                                <tr>
                                    <td>
                                        Fee from:
                                    </td>
                                    <td>
                                        <asp:DropDownList ID="ddlPaypalFeeCurrencyType" runat="server" Width="68px">
                                            <asp:ListItem Value="0">%</asp:ListItem>
                                        </asp:DropDownList>
                                        <num:NumericBox ID="txtPaypalFeeAmount" runat="server" CssClass="fee" />
                                        <asp:HiddenField ID="txtPaypalFeeExRate" runat="server" Value="0"></asp:HiddenField>
                                    </td>
                                    <td>
                                        to
                                    </td>
                                    <td>
                                        <num:NumericBox ID="txtPaypalFeeVNDAmount" runat="server" CssClass="subfee" Enabled="false" />
                                    </td>
                                </tr>
                                <tr>
                                    <td>
                                        Received by
                                    </td>
                                    <td colspan="3">
                                        <asp:TextBox ID="txtPaypalReceived" runat="server"></asp:TextBox>
                                    </td>
                                </tr>
                            </tbody>
                        </table>
                    </div>
                    <!-- CREDIT CARD -->
                    <div id="creditcard">
                        <table cellspacing="5">
                            <tbody>
                                <tr>
                                    <td>
                                        Conversion from
                                    </td>
                                    <td>
                                        <asp:DropDownList ID="ddlCreditCurrencyType" runat="server" Width="68px">
                                        </asp:DropDownList>
                                        <num:NumericBox ID="txtCreditAmount" runat="server" />
                                        <asp:HiddenField ID="txtCreditExRate" runat="server" Value="0"></asp:HiddenField>
                                    </td>
                                    <td>
                                        to
                                    </td>
                                    <td>
                                        <num:NumericBox ID="txtCreditVNDAmount" runat="server" CssClass="subpaid" Enabled="false" />
                                    </td>
                                </tr>
                                <tr>
                                    <td>
                                        Fee from:
                                    </td>
                                    <td>
                                        <asp:DropDownList ID="ddlCreditFeeCurrencyType" runat="server" Width="68px">
                                            <asp:ListItem Value="0">%</asp:ListItem>
                                        </asp:DropDownList>
                                        <num:NumericBox ID="txtCreditFeeAmount" runat="server" CssClass="fee" />
                                        <asp:HiddenField ID="txtCreditFeeExRate" runat="server" Value="0"></asp:HiddenField>
                                    </td>
                                    <td>
                                        to
                                    </td>
                                    <td>
                                        <num:NumericBox ID="txtCreditFeeVNDAmount" runat="server" CssClass="subfee" Enabled="false" />
                                    </td>
                                </tr>
                                <tr>
                                    <td>
                                        Received by
                                    </td>
                                    <td colspan="3">
                                        <asp:TextBox ID="txtCreditReceived" runat="server"></asp:TextBox>
                                    </td>
                                </tr>
                            </tbody>
                        </table>
                    </div>
                </div>
            </td>
        </tr>
        <tr>
            <td>
                <table cellspacing="10">
                    <tr>
                        <td>
                            <div class="item">
                                <div class="label" style="font-weight: bold; color: #f70">
                                    <asp:Label ID="lblCurrentPaid" runat="server" AssociatedControlID="txtCurrentPaid">Current Paid:</asp:Label>
                                </div>
                                <div class="field">
                                    <num:NumericBox ID="txtCurrentPaid" runat="server" CssClass="sub" Enabled="false" />
                                </div>
                            </div>
                        </td>
                        <td>
                            <div class="item">
                                <div class="label" style="font-weight: bold; color: #f70">
                                    <asp:Label ID="lblCurrentFee" runat="server" AssociatedControlID="txtCurrentFee">Current Fee:</asp:Label>
                                </div>
                                <div class="field">
                                    <num:NumericBox ID="txtCurrentFee" runat="server" CssClass="sub" Enabled="false" />
                                </div>
                            </div>
                        </td>
                    </tr>
                </table>
            </td>
        </tr>
        <tr>
            <td>
                <asp:HiddenField ID="hidPaymentBatchID" runat="server" />
                <asp:HiddenField ID="hidPaidDate" runat="server" />
                <div class="item">
                    <span id="spanNoticePopup">
                        <asp:Literal ID="litNoticePopup" runat="server" />
                    </span>
                </div>
            </td>
        </tr>
    </table>
</div>

<script type="text/javascript">

    var addFlag = false;

    // Total
    function getCurrentPaid() {
        var subpaid = $('input.subpaid').sumValues();
        currentPaidCtl.autoNumericSet(subpaid);
        getTotalFee();
    }

    function getTotalFee() {
        var subfee = $('input.subfee').sumValues();
        currentFeeCtl.autoNumericSet(subfee);
    }

    function getCashAmount(currency, rateNameCtl, amountNameCtl, amountVNNameCtl) {
        var rate = getCash(currency);
        if (rate != null) {
            $(rateNameCtl).val(rate.UnitPrice);
            $(amountVNNameCtl).autoNumericSet(toFloat($(amountNameCtl).val()) * toFloat($(rateNameCtl).val()));
        }

        getCurrentPaid();
    }

    function getAmount(currency, date, rateNameCtl, amountNameCtl, amountVNNameCtl) {
        var rate = getRate(currency);
        if (rate != null) {
            $(rateNameCtl).val(rate.UnitPrice);
            $(amountVNNameCtl).autoNumericSet(toFloat($(amountNameCtl).val()) * toFloat($(rateNameCtl).val()));
        }

        getCurrentPaid();
    }

    $(document).ready(function () {

        // Cash
        function getVNDAmount1() {
            getCashAmount($("#<%=ddlCurrencyType1.ClientID %> option:selected").text(),
                "#<%=txtExRate1.ClientID %>",
                "#<%=txtAmount1.ClientID %>",
                "#<%=txtVNDAmount1.ClientID %>");
        }
        $("#<%=txtAmount1.ClientID %>").blur(function () { getVNDAmount1() });
        $("#<%=ddlCurrencyType1.ClientID %>").change(function () { getVNDAmount1(); });

        function getVNDAmount2() {
            getCashAmount($("#<%=ddlCurrencyType2.ClientID %> option:selected").text(),
                "#<%=txtExRate2.ClientID %>",
                "#<%=txtAmount2.ClientID %>",
                "#<%=txtVNDAmount2.ClientID %>");
        }
        $("#<%=txtAmount2.ClientID %>").blur(function () { getVNDAmount2() });
        $("#<%=ddlCurrencyType2.ClientID %>").change(function () { getVNDAmount2(); });

        // German Account
        function getGermanVNDAmount() {
            getAmount($("#<%=ddlGermanCurrencyType.ClientID %> option:selected").text(),
                $("#<%=hidPaidDate.ClientID %>").val(),
                "#<%=txtGermanExRate.ClientID %>",
                "#<%=txtGermanAmount.ClientID %>",
                "#<%=txtGermanVNDAmount.ClientID %>");
        }
        $("#<%=txtGermanAmount.ClientID %>").blur(function () { getGermanVNDAmount(); });
        $("#<%=ddlGermanCurrencyType.ClientID %>").change(function () { getGermanVNDAmount(); });

        // Pay One
        function getOnepayVNDAmount1() {
            getAmount($("#<%=ddlOnepayCurrencyType1.ClientID %> option:selected").text(),
                $("#<%=hidPaidDate.ClientID %>").val(),
                "#<%=txtOnepayExRate1.ClientID %>",
                "#<%=txtOnepayAmount1.ClientID %>",
                "#<%=txtOnepayVNDAmount1.ClientID %>");
        }
        $("#<%=txtOnepayAmount1.ClientID %>").blur(function () { getOnepayVNDAmount1(); });
        $("#<%=ddlOnepayCurrencyType1.ClientID %>").blur(function () { getOnepayVNDAmount1(); });

        function getOnepayVNDAmount2() {
            getAmount($("#<%=ddlOnepayCurrencyType2.ClientID %> option:selected").text(),
                $("#<%=hidPaidDate.ClientID %>").val(),
                "#<%=txtOnepayExRate2.ClientID %>",
                "#<%=txtOnepayAmount2.ClientID %>",
                "#<%=txtOnepayVNDAmount2.ClientID %>");
        }
        $("#<%=txtOnepayAmount2.ClientID %>").blur(function () { getOnepayVNDAmount2(); });
        $("#<%=ddlOnepayCurrencyType2.ClientID %>").blur(function () { getOnepayVNDAmount2(); });

        // Charge Account
        function getChargeVNDAmount1() {
            getAmount($("#<%=ddlChargeCurrencyType1.ClientID %> option:selected").text(),
                $("#<%=hidPaidDate.ClientID %>").val(),
                "#<%=txtChargeExRate1.ClientID %>",
                "#<%=txtChargeAmount1.ClientID %>",
                "#<%=txtChargeVNDAmount1.ClientID %>");
        }
        $("#<%=txtChargeAmount1.ClientID %>").blur(function () { getChargeVNDAmount1(); });
        $("#<%=ddlChargeCurrencyType1.ClientID %>").change(function () { getChargeVNDAmount1(); });

        function getChargeVNDAmount2() {
            getAmount($("#<%=ddlChargeCurrencyType2.ClientID %> option:selected").text(),
                $("#<%=hidPaidDate.ClientID %>").val(),
                "#<%=txtChargeExRate2.ClientID %>",
                "#<%=txtChargeAmount2.ClientID %>",
                "#<%=txtChargeVNDAmount2.ClientID %>");
        }
        $("#<%=txtChargeAmount2.ClientID %>").blur(function () { getChargeVNDAmount2(); });
        $("#<%=ddlChargeCurrencyType2.ClientID %>").change(function () { getChargeVNDAmount2(); });

        // Paypal
        function getPaypalVNDAmount() {
            getAmount($("#<%=ddlPaypalCurrencyType.ClientID %> option:selected").text(),
                $("#<%=hidPaidDate.ClientID %>").val(),
                "#<%=txtPaypalExRate.ClientID %>",
                "#<%=txtPaypalAmount.ClientID %>",
                "#<%=txtPaypalVNDAmount.ClientID %>");
        }
        $("#<%=txtPaypalAmount.ClientID %>").blur(function () { getPaypalVNDAmount(); });
        $("#<%=ddlPaypalCurrencyType.ClientID %>").change(function () { getPaypalVNDAmount(); });

        function getPaypalFeeVNDAmount() {

            if ($("#<%=ddlPaypalFeeCurrencyType.ClientID %>").val() == 0) {
                // Percent
                $("#<%=txtPaypalFeeVNDAmount.ClientID %>").autoNumericSet(toFloat($("#<%=txtPaypalFeeAmount.ClientID %>").val()) * toFloat($("#<%=txtPaypalVNDAmount.ClientID %>").val()) / 100);
                getCurrentPaid();
            }
            else {
                getAmount($("#<%=ddlPaypalFeeCurrencyType.ClientID %> option:selected").text(),
                    $("#<%=hidPaidDate.ClientID %>").val(),
                    "#<%=txtPaypalFeeExRate.ClientID %>",
                    "#<%=txtPaypalFeeAmount.ClientID %>",
                    "#<%=txtPaypalFeeVNDAmount.ClientID %>");
            }
        }

        $("#<%=txtPaypalFeeAmount.ClientID %>").blur(function () { getPaypalFeeVNDAmount() });
        $("#<%=ddlPaypalFeeCurrencyType.ClientID %>").change(function () { getPaypalFeeVNDAmount(); });
        $("#<%=txtPaypalFeeExRate.ClientID %>").blur(function () { if ($("#<%=ddlPaypalFeeCurrencyType.ClientID %>").val() == 0) getPaypalFeeVNDAmount(); });

        // Credit
        function getCreditVNDAmount() {
            getAmount($("#<%=ddlCreditCurrencyType.ClientID %> option:selected").text(),
                    $("#<%=hidPaidDate.ClientID %>").val(),
                    "#<%=txtCreditExRate.ClientID %>",
                    "#<%=txtCreditAmount.ClientID %>",
                    "#<%=txtCreditVNDAmount.ClientID %>");
        }
        $("#<%=txtCreditAmount.ClientID %>").blur(function () { getCreditVNDAmount(); });
        $("#<%=ddlCreditCurrencyType.ClientID %>").change(function () { getCreditVNDAmount(); });

        function getCreditFeeVNDAmount() {

            if ($("#<%=ddlCreditFeeCurrencyType.ClientID %>").val() == 0) {
                // Percent
                $("#<%=txtCreditFeeVNDAmount.ClientID %>").autoNumericSet(toFloat($("#<%=txtCreditFeeAmount.ClientID %>").val()) * toFloat($("#<%=txtCreditVNDAmount.ClientID %>").val()) / 100);
                getCurrentPaid();
            }
            else {
                getAmount($("#<%=ddlCreditFeeCurrencyType.ClientID %> option:selected").text(),
                    $("#<%=hidPaidDate.ClientID %>").val(),
                    "#<%=txtCreditFeeExRate.ClientID %>",
                    "#<%=txtCreditFeeAmount.ClientID %>",
                    "#<%=txtCreditFeeVNDAmount.ClientID %>");
            }
        }

        $("#<%=txtCreditFeeAmount.ClientID %>").blur(function () { getCreditFeeVNDAmount() });
        $("#<%=ddlCreditFeeCurrencyType.ClientID %>").change(function () { getCreditFeeVNDAmount(); });
        $("#<%=txtCreditFeeExRate.ClientID %>").blur(function () { if ($("#<%=ddlCreditFeeCurrencyType.ClientID %>").val() == 0) getCreditFeeVNDAmount(); });

        $('input.subpaid').blur(function () {
            getCurrentPaid();
        });
        $('input.subfee').blur(function () {
            getTotalFee();
        });

        $('input.sub').autoNumeric({ aSep: ',', aDec: '.', aPad: false, aSign: ' VND', pSign: 's', vMin: '-999999999.99' });
    });


    var 
    // Sub-Total
        hidPaymentBatchID = $("#<%=hidPaymentBatchID.ClientID %>"),
        currentPaidCtl = $("#<%=txtCurrentPaid.ClientID %>"),
        currentFeeCtl = $("#<%=txtCurrentFee.ClientID %>");

    function buildRow(method, currency, amount, unitOfFee, fee, exchangeRate, rateOfFee,
                currencyId, unitOfFeeId, methodId, receiver) {

        var row = stringformat('<tr><td>{0}</td>' +
                            '<td style="text-align: right">{1}</td>' +
                            '<td>{2}</td>' +
                            '<td style="text-align: right">{3}</td>' +
                            '<td>{4}</td>' +
                            '<td style="display: none">{5}</td>' +
                            '<td style="display: none">{6}</td>' +
                            '<td>{7}</td>' +
                            '<td style="display: none">{8}</td>' +
                            '<td style="display: none"></td>' +
                            '<td style="display: none">{9}</td>' +
                            '<td style="display: none">{10}</td></tr>',
                            method,
                            amount,
                            (currency == null ? '' : currency),
                            fee,
                            (unitOfFee == null ? '' : unitOfFee),
                            exchangeRate,
                            rateOfFee,
                            receiver,
                            methodId,
                            currencyId,
                            unitOfFeeId);
        return row;
    }

    function buildTable() {
        try {
            var methodId, method, currency, currencyId, exchangeRate, amount, total;
            var unitOfFee, unitOfFeeId, fee, rateOfFee, creditCard, receiver;
            var html, table = '';

            // Cash
            total = $("#<%=txtVNDAmount1.ClientID %>").autoNumericGet();

            
            if (total != 0) {
                methodId = 0;
                method = "Cash";
                currencyId = $("#<%=ddlCurrencyType1.ClientID %> option:selected").val();
                currency = $("#<%=ddlCurrencyType1.ClientID %> option:selected").text();
                amount = $("#<%=txtAmount1.ClientID %>").val();
                exchangeRate = $("#<%=txtExRate1.ClientID %>").val();
                receiver = $("#<%=txtCashReceived.ClientID %>").val();
                // No fee
                fee = 0;
                rateOfFee = 1;
                unitOfFee = null;
                unitOfFeeId = 0;

                table += buildRow(method, currency, amount, unitOfFee, fee, exchangeRate,
            rateOfFee, currencyId, unitOfFeeId, methodId, receiver);
            }

            total = $("#<%=txtVNDAmount2.ClientID %>").autoNumericGet();

            if (total != 0) {
                methodId = 0;
                method = "Cash";
                currencyId = $("#<%=ddlCurrencyType2.ClientID %> option:selected").val();
                currency = $("#<%=ddlCurrencyType2.ClientID %> option:selected").text();
                amount = $("#<%=txtAmount2.ClientID %>").val();
                exchangeRate = $("#<%=txtExRate2.ClientID %>").val();
                receiver = $("#<%=txtCashReceived.ClientID %>").val();
                // No fee
                fee = 0;
                rateOfFee = 1;
                unitOfFee = null;
                unitOfFeeId = 0;

                table += buildRow(method, currency, amount, unitOfFee, fee, exchangeRate,
            rateOfFee, currencyId, unitOfFeeId, methodId, receiver);
            }
            // German

            total = $("#<%=txtGermanVNDAmount.ClientID %>").autoNumericGet();

            if (total != 0) {
                methodId = 1;
                method = "German";
                currencyId = $("#<%=ddlGermanCurrencyType.ClientID %> option:selected").val();
                currency = $("#<%=ddlGermanCurrencyType.ClientID %> option:selected").text();
                amount = $("#<%=txtGermanAmount.ClientID %>").val();
                exchangeRate = $("#<%=txtGermanExRate.ClientID %>").val();
                receiver = $("#<%=txtGermanReceived.ClientID %>").val();
                // No fee
                fee = 0;
                rateOfFee = 1;
                unitOfFee = null;
                unitOfFeeId = 0;

                table += buildRow(method, currency, amount, unitOfFee, fee, exchangeRate,
            rateOfFee, currencyId, unitOfFeeId, methodId, receiver);
            }
            // Onepay

            total = $("#<%=txtOnepayVNDAmount1.ClientID %>").autoNumericGet();

            if (total != 0) {
                methodId = 2;
                method = "Onepay";
                currencyId = $("#<%=ddlOnepayCurrencyType1.ClientID %> option:selected").val();
                currency = $("#<%=ddlOnepayCurrencyType1.ClientID %> option:selected").text();
                amount = $("#<%=txtOnepayAmount1.ClientID %>").val();
                exchangeRate = $("#<%=txtOnepayExRate1.ClientID %>").val();
                receiver = $("#<%=txtOnepayReceived.ClientID %>").val();
                // No fee
                fee = 0;
                rateOfFee = 1;
                unitOfFee = null;
                unitOfFeeId = 0;

                table += buildRow(method, currency, amount, unitOfFee, fee, exchangeRate,
            rateOfFee, currencyId, unitOfFeeId, methodId, receiver);
            }

            total = $("#<%=txtOnepayVNDAmount2.ClientID %>").autoNumericGet();

            if (total != 0) {
                methodId = 2;
                method = "Onepay";
                currencyId = $("#<%=ddlOnepayCurrencyType2.ClientID %> option:selected").val();
                currency = $("#<%=ddlOnepayCurrencyType2.ClientID %> option:selected").text();
                amount = $("#<%=txtOnepayAmount2.ClientID %>").val();
                exchangeRate = $("#<%=txtOnepayExRate2.ClientID %>").val();
                receiver = $("#<%=txtOnepayReceived.ClientID %>").val();
                // No fee
                fee = 0;
                rateOfFee = 1;
                unitOfFee = null;
                unitOfFeeId = 0;

                table += buildRow(method, currency, amount, unitOfFee, fee, exchangeRate,
            rateOfFee, currencyId, unitOfFeeId, methodId, receiver);
            }
            // Charge Account

            total = $("#<%=txtChargeVNDAmount1.ClientID %>").autoNumericGet();

            if (total != 0) {
                methodId = 3;
                method = "Trả sau";
                currencyId = $("#<%=ddlChargeCurrencyType1.ClientID %> option:selected").val();
                currency = $("#<%=ddlChargeCurrencyType1.ClientID %> option:selected").text();
                amount = $("#<%=txtChargeAmount1.ClientID %>").val();
                exchangeRate = $("#<%=txtChargeExRate1.ClientID %>").val();
                receiver = $("#<%=txtChargeReceived.ClientID %>").val();
                // No fee
                fee = 0;
                rateOfFee = 1;
                unitOfFee = null;
                unitOfFeeId = 0;

                table += buildRow(method, currency, amount, unitOfFee, fee, exchangeRate,
            rateOfFee, currencyId, unitOfFeeId, methodId, receiver);
            }

            total = $("#<%=txtChargeVNDAmount2.ClientID %>").autoNumericGet();

            if (total != 0) {
                methodId = 3;
                method = "Trả sau";
                currencyId = $("#<%=ddlChargeCurrencyType2.ClientID %> option:selected").val();
                currency = $("#<%=ddlChargeCurrencyType2.ClientID %> option:selected").text();
                amount = $("#<%=txtChargeAmount2.ClientID %>").val();
                exchangeRate = $("#<%=txtChargeExRate2.ClientID %>").val();
                receiver = $("#<%=txtChargeReceived.ClientID %>").val();
                // No fee
                fee = 0;
                rateOfFee = 1;
                unitOfFee = null;
                unitOfFeeId = 0;

                table += buildRow(method, currency, amount, unitOfFee, fee, exchangeRate,
            rateOfFee, currencyId, unitOfFeeId, methodId, receiver);
            }

            // Pay Pal

            total = $("#<%=txtPaypalVNDAmount.ClientID %>").autoNumericGet();

            if (total != 0) {
                methodId = 4;
                method = "Pay Pal";
                currencyId = $("#<%=ddlPaypalCurrencyType.ClientID %> option:selected").val();
                currency = $("#<%=ddlPaypalCurrencyType.ClientID %> option:selected").text();
                amount = $("#<%=txtPaypalAmount.ClientID %>").val();
                exchangeRate = $("#<%=txtPaypalExRate.ClientID %>").val();
                receiver = $("#<%=txtPaypalReceived.ClientID %>").val();
                // No fee
                fee = $("#<%=txtPaypalFeeAmount.ClientID %>").val();
                rateOfFee = $("#<%=txtPaypalFeeExRate.ClientID %>").val();
                unitOfFee = $("#<%=ddlPaypalFeeCurrencyType.ClientID %> option:selected").text();
                unitOfFeeId = $("#<%=ddlPaypalFeeCurrencyType.ClientID %> option:selected").val();

                table += buildRow(method, currency, amount, unitOfFee, fee, exchangeRate,
            rateOfFee, currencyId, unitOfFeeId, methodId, receiver);
            }

            // Credit Card

            total = $("#<%=txtCreditVNDAmount.ClientID %>").autoNumericGet();

            if (total != 0) {
                methodId = 5;
                method = "Credit Card";
                currencyId = $("#<%=ddlCreditCurrencyType.ClientID %> option:selected").val();
                currency = $("#<%=ddlCreditCurrencyType.ClientID %> option:selected").text();
                amount = $("#<%=txtCreditAmount.ClientID %>").val();
                exchangeRate = $("#<%=txtCreditExRate.ClientID %>").val();
                receiver = $("#<%=txtCreditReceived.ClientID %>").val();
                // No fee
                fee = $("#<%=txtCreditFeeAmount.ClientID %>").val();
                rateOfFee = $("#<%=txtCreditFeeExRate.ClientID %>").val();
                unitOfFee = $("#<%=ddlCreditFeeCurrencyType.ClientID %> option:selected").text();
                unitOfFeeId = $("#<%=ddlCreditFeeCurrencyType.ClientID %> option:selected").val();

                table += buildRow(method, currency, amount, unitOfFee, fee, exchangeRate,
            rateOfFee, currencyId, unitOfFeeId, methodId, receiver);
            }

            if (table != '') {
                html = '<table class="grid" border="0" cellspacing="0">';
                html += '<thead><th>Method</th><th colspan="2">Price</th><th colspan="2">Fee</th><th>Received</th></thead>';
                html += '<tbody>' + table + '</tbody>';
                html += '</table>';
            }
            return html;

        }
        catch (err) {
            alert(err.message);
        }
    }

    $("#payment-dialog").dialog({
        autoOpen: false,
        width: 668,
        modal: true,
        buttons: {
            "OK": function () {

                var bValid = true;

                if (bValid) {
                    try {

                        var amount = $("#<%=txtCurrentPaid.ClientID %>").autoNumericGet();
                        if (amount != 0) {
                            // Paid occurs.
                            var tbody = $('#gridPaymentHistory > tbody');
                            var seq = ($('tr', tbody).length + 1);
                            var evenClass = (seq % 2 == 0 ? ' class="evenRow special"' : ' class="special"');
                            // special for the payment grid.

                            var fee = $("#<%=txtCurrentFee.ClientID %>").autoNumericGet();
                            var html = buildTable();

                            if (hidPaymentBatchID.val() != '') {
                                // edit
                                $('tr.special:contains(' + hidPaymentBatchID.val() + ')', tbody).children("td:eq(2)").text(amount);
                                $('tr.special:contains(' + hidPaymentBatchID.val() + ')', tbody).children("td:eq(3)").text(fee);
                                $('tr.special:contains(' + hidPaymentBatchID.val() + ')', tbody).children("td:eq(5)").html(html);
                            }
                            else {

                                if (addFlag)
                                    tbody.html('');

                                var row = stringformat('<tr{0}><td style="display: none">{1}</td>' +
                                                        '<td>{2}</td><td style="text-align: right">{3}</td>' +
                                                        '<td style="text-align: right">{4}</td>' +
                                                        '<td>{5}</td><td>{6}</td>' + 
                                                        '<td><a paymentid="{1}" class="btnDelete new">Remove</a></td></tr>',
                                                        evenClass,
                                                        hidPaymentBatchID.val(),
                                                        dateFormat(new Date(), "mm/dd/yyyy"),
                                                        formatCurrency(amount),
                                                        formatCurrency(fee),
                                                        addFlag ? 'Yes' : '',
                                                        html);

                                if ($.trim(tbody.html()) == '') {
                                    tbody.html(row);
                                }
                                else {
                                    // Insert
                                    $('tr.special:last', tbody).after(row);
                                }

                                paymentEvents();
                            }

                            $("#litNoticePopup").text("Paid successfully.");

                            // Update current paid
                            getSubTotal();

                            $(this).dialog("close");
                        }
                    }
                    catch (e) {
                        $("#litNoticePopup").text("Paid failed!");
                    }
                }
            },
            Cancel: function () {
                $(this).dialog("close");
            }
        },
        close: function () {
        }
    });

    /// <summary>
    /// Payment Events
    /// </summary>
    function paymentEvents() {
        // Edit
        $("#gridPaymentHistory a.btnEdit.new").click(function () {

            var idx = $("#gridPaymentHistory > tbody > tr").index($(this).closest('tr'));

            editPayment(idx);

            $("#payment-dialog").show();
            $("#payment-dialog").dialog("open");
        });

        $("#gridPaymentHistory a.btnEdit").removeClass("new");

        // Delete
        $("#gridPaymentHistory a.btnDelete.new").click(function () {
            if (confirm('Are you sure you want to delete this payment batch?')) {
                $(this).closest('tr').remove();
                getSubTotal();
            }
        });

        $("#gridPaymentHistory a.btnDelete").removeClass("new");
    }

    function clearPayment() {
        hidPaymentBatchID.val('');
        $("#<%=hidPaidDate.ClientID %>").val(new Date());
        $("#<%=txtCurrentPaid.ClientID %>").val(0);

        // Cash 1
        $("#<%=txtAmount1.ClientID %>").val(0);
        $("#<%=txtVNDAmount1.ClientID %>").val(0);
        $("#<%=ddlCurrencyType1.ClientID %>").val(-1);
        $("#<%=txtExRate1.ClientID %>").val(0);

        // Cash 2
        $("#<%=txtAmount2.ClientID %>").val(0);
        $("#<%=txtVNDAmount2.ClientID %>").val(0);
        $("#<%=ddlCurrencyType2.ClientID %>").val(-1);
        $("#<%=txtExRate2.ClientID %>").val(0);

        // German
        $("#<%=txtGermanAmount.ClientID %>").val(0);
        $("#<%=txtGermanVNDAmount.ClientID %>").val(0);
        $("#<%=ddlGermanCurrencyType.ClientID %>").val(-1);
        $("#<%=txtGermanExRate.ClientID %>").val(0);

        // Onepay 1
        $("#<%=txtOnepayAmount1.ClientID %>").val(0);
        $("#<%=txtOnepayVNDAmount1.ClientID %>").val(0);
        $("#<%=ddlOnepayCurrencyType1.ClientID %>").val(-1);
        $("#<%=txtOnepayExRate1.ClientID %>").val(0);

        // Onepay 2
        $("#<%=txtOnepayAmount2.ClientID %>").val(0);
        $("#<%=txtOnepayVNDAmount2.ClientID %>").val(0);
        $("#<%=ddlOnepayCurrencyType2.ClientID %>").val(-1);
        $("#<%=txtOnepayExRate2.ClientID %>").val(0);

        // Charge 1
        $("#<%=txtChargeAmount1.ClientID %>").val(0);
        $("#<%=txtChargeVNDAmount1.ClientID %>").val(0);
        $("#<%=ddlChargeCurrencyType1.ClientID %>").val(-1);
        $("#<%=txtChargeExRate1.ClientID %>").val(0);

        // Charge 2
        $("#<%=txtChargeAmount2.ClientID %>").val(0);
        $("#<%=txtChargeVNDAmount2.ClientID %>").val(0);
        $("#<%=ddlChargeCurrencyType2.ClientID %>").val(-1);
        $("#<%=txtChargeExRate2.ClientID %>").val(0);

        // Pay pal
        $("#<%=txtPaypalAmount.ClientID %>").val(0);
        $("#<%=txtPaypalVNDAmount.ClientID %>").val(0);
        $("#<%=ddlPaypalCurrencyType.ClientID %>").val(-1);
        $("#<%=txtPaypalExRate.ClientID %>").val(0);

        // Fee
        $("#<%=txtPaypalFeeAmount.ClientID %>").val(0);
        $("#<%=txtPaypalFeeVNDAmount.ClientID %>").val(0);
        $("#<%=ddlPaypalFeeCurrencyType.ClientID %>").val(-1);
        $("#<%=txtPaypalFeeExRate.ClientID %>").val(0);

        // Credit
        $("#<%=txtCreditAmount.ClientID %>").val(0);
        $("#<%=txtCreditVNDAmount.ClientID %>").val(0);
        $("#<%=ddlCreditCurrencyType.ClientID %>").val(-1);
        $("#<%=txtCreditExRate.ClientID %>").val(0);

        // Fee
        $("#<%=txtCreditFeeAmount.ClientID %>").val(0);
        $("#<%=txtCreditFeeVNDAmount.ClientID %>").val(0);
        $("#<%=ddlCreditFeeCurrencyType.ClientID %>").val(-1);
        $("#<%=txtCreditFeeExRate.ClientID %>").val(0);
    }

    function editPayment(idx) {
        var line = $("#gridPaymentHistory > tbody > tr:eq(" + idx + ")");

        // 0 : Date
        // 1 : Details Table

        hidPaymentBatchID.val($(line).children("td:eq(0)").text());
        $("#<%=hidPaidDate.ClientID %>").val($(line).children("td:eq(1)").text());
        $("#<%=txtCurrentPaid.ClientID %>").autoNumericSet(toFloat($(line).children("td:eq(2)").text()));
        $("#<%=txtCurrentFee.ClientID %>").autoNumericSet(toFloat($(line).children("td:eq(3)").text()));

        var iCash = 1;
        var iOnepay = 1;
        var iCharge = 1;

        // not first is skip header
        $("tr:not(:first)", $(line).children("td:eq(5)").html()).each(function () {

            var method = parseInt($(this).children("td:eq(8)").text());
            var amount = toFloat($(this).children("td:eq(1)").text());
            var rate = toFloat($(this).children("td:eq(5)").text());
            var currency = parseInt($(this).children("td:eq(10)").text());
            var totalAmount = amount * rate;

            var fee = toFloat($(this).children("td:eq(3)").text());
            var feeRate = toFloat($(this).children("td:eq(6)").text());
            var feeCurrency = parseInt($(this).children("td:eq(11)").text());
            var totalFee = fee * feeRate;
            if (feeCurrency == 0) totalFee = fee * totalAmount / 100;

            switch (method) {
                case 0:
                    // Cash
                    if (iCash == 1) {
                        $("#<%=txtAmount1.ClientID %>").autoNumericSet(amount);
                        $("#<%=txtExRate1.ClientID %>").autoNumericSet(rate);
                        $("#<%=ddlCurrencyType1.ClientID %>").val(currency);
                        $("#<%=txtVNDAmount1.ClientID %>").autoNumericSet(totalAmount);
                    }
                    else {
                        $("#<%=txtAmount2.ClientID %>").autoNumericSet(amount);
                        $("#<%=txtExRate2.ClientID %>").autoNumericSet(rate);
                        $("#<%=ddlCurrencyType2.ClientID %>").val(currency);
                        $("#<%=txtVNDAmount2.ClientID %>").autoNumericSet(totalAmount);
                    }
                    iCash++;
                    break;
                case 1:
                    // German
                    $("#<%=txtGermanAmount.ClientID %>").autoNumericSet(amount);
                    $("#<%=txtGermanExRate.ClientID %>").autoNumericSet(rate);
                    $("#<%=ddlGermanCurrencyType.ClientID %>").val(currency);
                    $("#<%=txtGermanVNDAmount.ClientID %>").autoNumericSet(totalAmount);

                    break;
                case 2:
                    // Onepay
                    if (iOnepay == 1) {
                        $("#<%=txtOnepayAmount1.ClientID %>").autoNumericSet(amount);
                        $("#<%=ddlOnepayCurrencyType1.ClientID %>").val(currency);
                        $("#<%=txtOnepayExRate1.ClientID %>").autoNumericSet(rate);
                        $("#<%=txtOnepayVNDAmount1.ClientID %>").autoNumericSet(totalAmount);
                    }
                    else {
                        $("#<%=txtOnepayAmount2.ClientID %>").autoNumericSet(amount);
                        $("#<%=ddlOnepayCurrencyType2.ClientID %>").val(currency);
                        $("#<%=txtOnepayExRate2.ClientID %>").autoNumericSet(rate);
                        $("#<%=txtOnepayVNDAmount2.ClientID %>").autoNumericSet(totalAmount);
                    }
                    iOnepay++;
                    break;
                case 3:
                    // Charge
                    if (iCharge == 1) {
                        $("#<%=txtChargeAmount1.ClientID %>").autoNumericSet(amount);
                        $("#<%=ddlChargeCurrencyType1.ClientID %>").val(currency);
                        $("#<%=txtChargeExRate1.ClientID %>").autoNumericSet(rate);
                        $("#<%=txtChargeVNDAmount1.ClientID %>").autoNumericSet(totalAmount);
                    }
                    else {
                        $("#<%=txtChargeAmount2.ClientID %>").autoNumericSet(amount);
                        $("#<%=ddlChargeCurrencyType2.ClientID %>").val(currency);
                        $("#<%=txtChargeExRate2.ClientID %>").autoNumericSet(rate);
                        $("#<%=txtChargeVNDAmount2.ClientID %>").autoNumericSet(totalAmount);
                    }
                    iCharge++;
                    break;
                case 4:
                    // Pay pal
                    $("#<%=txtPaypalAmount.ClientID %>").autoNumericSet(amount);
                    $("#<%=ddlPaypalCurrencyType.ClientID %>").val(currency);
                    $("#<%=txtPaypalExRate.ClientID %>").autoNumericSet(rate);
                    $("#<%=txtPaypalVNDAmount.ClientID %>").autoNumericSet(totalAmount);

                    // Fee
                    $("#<%=txtPaypalFeeAmount.ClientID %>").autoNumericSet(fee);
                    $("#<%=ddlPaypalFeeCurrencyType.ClientID %>").val(feeCurrency);
                    $("#<%=txtPaypalFeeExRate.ClientID %>").autoNumericSet(feeRate);
                    $("#<%=txtPaypalFeeVNDAmount.ClientID %>").autoNumericSet(totalFee);
                    break;
                case 5:
                    // Credit
                    $("#<%=txtCreditAmount.ClientID %>").autoNumericSet(amount);
                    $("#<%=ddlCreditCurrencyType.ClientID %>").val(currency);
                    $("#<%=txtCreditExRate.ClientID %>").autoNumericSet(rate);
                    $("#<%=txtCreditVNDAmount.ClientID %>").autoNumericSet(totalAmount);

                    // Fee
                    $("#<%=txtCreditFeeAmount.ClientID %>").autoNumericSet(fee);
                    $("#<%=ddlCreditFeeCurrencyType.ClientID %>").val(feeCurrency);
                    $("#<%=txtCreditFeeExRate.ClientID %>").autoNumericSet(feeRate);
                    $("#<%=txtCreditFeeVNDAmount.ClientID %>").autoNumericSet(totalFee);
                    break;
                default:
                    // Other
                    break;
            }
        });
    }

    // Shared function
    function getPaymentBatches() {
        var payments = [];
        $("#gridPaymentHistory > tbody > tr.special").each(function () {
            // Some info need to save.
            var paymentBatch = {
                PaymentDate: $.trim($(this).children("td:eq(1)").text()),
                PaidAmount: toFloat($(this).children("td:eq(2)").text()),
                PaidFee: toFloat($(this).children("td:eq(3)").text()),
                DepositFlag: ($.trim($(this).children("td:eq(4)").text()) == 'Yes'),
                PaymentBatchDetails: []
            };

            $("tr:not(:first)", $(this).children("td:eq(5)").html()).each(function () {

                var detail = {
                    PaymentMethod: $(this).children("td:eq(8)").text(),
                    CardType: null,
                    Amount: toFloat($(this).children("td:eq(1)").text()),
                    CurrencyAmount: parseInt($(this).children("td:eq(10)").text()),
                    Rate: toFloat($(this).children("td:eq(5)").text()),
                    Fee: toFloat($(this).children("td:eq(3)").text()),
                    CurrencyFee: parseInt($(this).children("td:eq(11)").text()),
                    FeeRate: toFloat($(this).children("td:eq(6)").text()),
                    ReceivedBy: $.trim($(this).children("td:eq(7)").text())
                };

                paymentBatch.PaymentBatchDetails.push(detail);
            });

            payments.push(paymentBatch);
        });

        return payments;
    }
</script>

